/**
 * 
 */
package com.avritech.zapsaude.test;

import javax.annotation.Resource;

import org.org.usurper.model.HandledBeanProperty;
import org.org.usurper.model.HandledConstructorArg;
import org.org.usurper.model.PropertyTypeDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import coop.tecso.core.persistence.model.PersistentObject;
import coop.tecso.core.service.Service;
import coop.tecso.core.test.handlers.ValueObjectHandler;
import coop.tecso.core.test.utils.SpringContextHelper;

/**
 * @author daniel
 *
 */
@Component("persistentObjectPropertyHandler")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class PersistentObjectPropertyHandler extends ValueObjectHandler {

    @SuppressWarnings("rawtypes")
    private Service service;

    @Resource(name = "springContextHelper")
    private SpringContextHelper springContextHelper;

    private Class<?> serviceClass;

    /**
     * 
     */
    public PersistentObjectPropertyHandler(Class<?> clazz) {
        super(new PropertyTypeDefinition(clazz));
        serviceClass = clazz;
    }

    /**
     * 
     */
    public PersistentObjectPropertyHandler(Class<?> clazz1, Class<?> clazz2) {
        super(clazz1, clazz2);
        serviceClass = clazz2;
    }

    /*
     * (non-Javadoc)
     * @see
     * org.org.usurper.handlers.additional.ValueObjectPropertyTypeHandler#handle(org.org.usurper.model
     * .HandledBeanProperty)
     */
    @Override
    public Object handle(HandledBeanProperty handledBeanProperty) {
        if (!handledBeanProperty.getPropertyClass().equals(serviceClass)) {
            handledBeanProperty = new HandledBeanProperty(handledBeanProperty.getTargetObject(), serviceClass,
                    handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
        }
        PersistentObject handled = (PersistentObject) super.handle(handledBeanProperty);
        return doHandle(handled);
    }

    /*
     * (non-Javadoc)
     * @see
     * org.org.usurper.handlers.additional.ValueObjectPropertyTypeHandler#handle(org.org.usurper.model
     * .HandledConstructorArg)
     */
    @Override
    public Object handle(HandledConstructorArg handledConstructorArg) {
        if (!handledConstructorArg.getConstructorArgClass().equals(serviceClass)) {
            handledConstructorArg = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), serviceClass,
                    handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
        }
        PersistentObject handled = (PersistentObject) super.handle(handledConstructorArg);
        return doHandle(handled);
    }

    @SuppressWarnings("unchecked")
    private PersistentObject doHandle(PersistentObject po) {
        po.setId(null);
        po.setCreationTimestamp(null);
        po.setCreationUser(null);
        po.setDeleted(false);
        po.setModificationTimestamp(null);
        po.setModificationUser(null);
        po.setVersionNumber(0);
        po = getService().save(po);
        return po;
    }

    @SuppressWarnings("rawtypes")
    private Service getService() {
        if (service == null) {
            service = springContextHelper.getService(serviceClass);
        }
        return service;
    }
}
